// pages/index/address/address.js
// 引入腾讯地图
const qqMapWxUtil = require('../../../utils/qqMapWxUtil.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: '',
    markers: [],
    region:[],
    form: '',
    addressDetail: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(`这里是从哪里进来的：` + options.form);
    console.log(`携带的地址信息是：` + options.address);
    console.log(`携带的地址信息是：` + options.addressDetail);
    this.setData({
      address: options.address,
      addressDetail: options.addressDetail,
      form: options.form
    })
    this.geocoder(this.data.address)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 调用腾讯地图进行定位
   * address:地址信息
   */
  geocoder: function (address) {
    let that = this
    if (!address) {
      console.log(`地址为空，不进行操作了！！！！` + address)
      retrun;
    }
    qqMapWxUtil.geocoder(address).then(response => {
      that.reverseGeocoder(response.result.location.lat, response.result.location.lng)
      let region = [];
      region.push(response.result.address_components.province)
      region.push(response.result.address_components.city)
      region.push(response.result.address_components.district)
      that.setData({
        region: region
      })
    })
  },
  /**
  *调用腾讯地图显示位置信息
  * latitude:纬度
  * longitude：经度
  */
  reverseGeocoder: function (latitude, longitude) {
    let that = this
    let location = {}
    location.latitude = latitude;
    location.longitude = longitude;
    qqMapWxUtil.reverseGeocoder(location).then(response => {
      console.log(`从腾讯文档中获取到的地图坐标是：`, response)
      that.setData({
        markers: response.markers
      })
    })
  },
  /**
   * 地址选择
   */
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value,
      address: e.detail.value[0] + e.detail.value[1] + e.detail.value[2]
    })
    this.geocoder(e.detail.value)
  },
  /**
   * 点击地图重新定位
   */
  queyraddress: function (eventhandle) {
    console.log(eventhandle)
  },
  /**
   * 保存地址
   */
  saveAddress:function() {
    console.log(`来到了保存界面` + this.data.form)
    console.log(`地址详情` + this.data.addressDetail)
    if (this.data.form === 'consume') {
      
      // 来自记账的
      wx.reLaunch({
        url: '../../consume/consume?form=address&address=' + this.data.address + '&addressDetail=' + this.data.addressDetail
      })
    } else if(this.data.form === 'bookkeepingDetail') {
      console.log(`匹配到了bookkeepingDetail`)
      // 来自记账详情的
      wx.reLaunch({
        url: '../bookkeepingdetail/bookkeepingdetail?form=address&address=' + this.data.address + '&addressDetail=' + this.data.addressDetail,
      })
    }
  },
  textareaAddressDetail:function(e) {
    console.log(`获取到数据了`, e.detail.value)
    let addressDetail = e.detail.value
    this.setData({
      addressDetail: addressDetail
    })
  }
})